Next | Prev | Up | Top | Contents | Index

BSD Timer Support

As described under "Timer Interrupts (Itimers)", IRIX supports the BSD UNIX functions for interval timing, which defines three kinds of software interval timers. All three are used the same way:

  1. The program calls setitimer(), specifying the timer, a time duration, and an optional repeat duration.

  2. The kernel counts down the time interval against some time base.

  3. When the interval has elapsed, the kernel generates a signal to the process, and optionally starts a new interval of the repeat duration.
The three kinds of timers differ in the time base they use, the precision with which you can specify the intervals, and in the signals they send, as summarized in Table 5-2.

Types of itimer
Kind of itimerInterval MeasuredResolutionSignal Sent
ITIMER_REALElapsed clock time1 millisecond or lessSIGALRM
ITIMER_VIRTUALUser time (process execution time)1 secondSIGVTALRM
ITIMER_PROFUser+system time1 secondSIGPROF

Of the three, only the ITIMER_REAL, which measures elapsed time, is useful for real-time processes. The signal it generates, SIGALRM, is always delivered as soon as possible. (Other signals are not delivered until a scheduling interval occurs; see "Signal Delivery and Latency".)

Note: Interval timers are not normally used with the Frame Scheduler. See "Using Timers with the Frame Scheduler"


Using an Itimer

Example 5-1 shows an outline of code to initialize a repeating timer signal.

Example 5-1 : Timer Initialization

usema_t * alarmSema; /* initialized elsewhere */
void uponSigalrm()
{
   usvsema(alarmSema); /* count a period */
}
int setUpTimer(long periodInMicrosec)
{
   struct sigaction alarmActor = {SA_RESTART,uponSigalrm,0};
   struct itimerval period = {{0, 0}, {0, 0}};
   if (sigaction(SIGALRM, & alarmActor, NULL))
   {
      perror("sigaction");
      return -1;
   }
   period.it_interval.tv_usec = periodInMicrosec;
   period.it_value.tv_usec = periodInMicrosec;
   if (setitimer(ITIMER_REAL, &period, NULL)) 
   {
      perror("setitimer");
      return -1;
   }
   return 0; /* indicate success */
}
The example begins by defining a signal-handling function, uponSigalrm(). This handler performs the V (count-up, revive) operation on a semaphore. When the main process has completed its work for one interval and is ready to wait until the next interval, it will perform the P (count-down, deplete) operation on the same semaphore. (This is one of many possibilities for interaction between the signal handler and the main process. For an example of another method based on sigsuspend(), see "Interprocess Communication" in Appendix A.)

The periodic timer is initialized in the function setUpTimer(). It establishes uponSigalrm() as the signal handler. Then it initializes the itimer, using a period passed as an argument.


Next | Prev | Up | Top | Contents | Index